    
import copy
import datetime
import pdb

from django import newforms as forms

from wjContact.contacts.exceptions import MultiWidgetException
  
from django.newforms.forms import BoundField as OldBoundField

class BoundField(OldBoundField):
    ## We are  just over-riding the as_widget method, to allow it to
    ## get the data for a multiwidget out of self.form.initial correctly.
    def as_widget(self, widget, attrs=None):
        attrs = attrs or {}
        auto_id = self.auto_id
        if auto_id and not attrs.has_key('id') and not widget.attrs.has_key('id'):
            attrs['id'] = auto_id
        if not self.form.is_bound:
            if isinstance(self.field, forms.MultiValueField):
                try:
                    data = self.field.widget.value_from_datadict(self.form.initial, self.name) 
                except MultiWidgetException:
                    print "Caught MultiWidgetException in BoundField class."
                    data = self.field.initial
                
                
            else:
                data = self.form.initial.get(self.name, self.field.initial)
        else:
            data = self.data
        return widget.render(self.html_name, data, attrs=attrs)     


class FieldList(object):
    def __init__(self, fields=None, template_field=None, base_name=None, 
        form_if_bound=None, fk_fieldname=None, model_class=None):
        #pdb.set_trace()
        self.fields_list = []
        if base_name:
            self.base_name = base_name
        if fields:
            self.fields_list = fields
        ## We wil allow just a template_field, or a template_field and a 
        ## how_many, but not neither of these, and not just a how_many on its
        ## own    
        self.template_field = template_field
        self.fk_fieldname = fk_fieldname
        self.model_class = model_class
        self.form_if_bound = form_if_bound
    
    def build_fields(self, number):               
        self.fields_list = []
        ## May need think about the creation counter in the copies at some 
        ## point.
        self.add_fields(number=number)
        
    def add_fields(self, number):
        if not self.template_field:
            self.template_field=self.fields_list[0]
        
        for i in range(number):
            self.fields_list.append(copy.deepcopy(self.template_field))
            # self.fields_list[i].initial="fred#fred#"
            
    def set_initial_values(self, values):
        for i, field in enumerate(self.fields_list):
            field.initial = values[i]
                   
    def get_names_and_fields(self):
        #pdb.set_trace()
        names_and_fields = []
        for i, field in enumerate(self.fields_list):
            name = self.base_name + '_%s' % (i+1)
            names_and_fields.append((name, field))
        return names_and_fields 
    names_and_fields = property(get_names_and_fields)
    
    def set_form(self, form):
        self.form_if_bound = form
    
    def __iter__(self):
        bound_fields = []
        for name, field in self.names_and_fields:
            yield BoundField(self.form_if_bound, field, name)
       
       





